/**
 * Created by zhangQ on 2017/8/6.
 * 1.主要采用lodash用来搜索数据
 */
import _ from 'lodash/lodash'

/**
 * TODO(搜索数据)
 * @param source:::数据源
 * @auther zhangQ
 * @date 2017/8/7 20:58
 **/
export function searchSourceDate (source, txt, searchField) {
  let resList = []
  if (source.length > 0) {
    let upTxt = txt.toUpperCase()
    let lowTxt = txt.toLowerCase()
    resList = _.filter(source, function (sourceItem) {
      let fields = searchField.split(',')
      let isExist = false
      isExist = fields.some((field) => {
        return (_.includes(sourceItem[field], upTxt) || _.includes(sourceItem[field], lowTxt))
      })
      return isExist
    })
  }
  return resList
}

/**
 * TODO(函数执行时间)
 * @param func:::执行的函数
 * @return 执行时间的毫秒数
 * @auther zhangQ
 * @date 2017/8/7 21:46
 **/
export function funcExeTime (func) {
  let start = new Date().getTime() // 起始时间
  func() // 执行待测函数
  let end = new Date().getTime() // 接受时间
  return (end - start) + 'ms' // 返回函数执行需要时间
}

export default {
  funcExeTime,
  searchSourceDate
}

